//
//  DBHelper+FollowReading.m
//  KidReading
//
//  Created by YangShuai on 16/9/27.
//  Copyright © 2016年 刘赞黄Telen. All rights reserved.
//

#import "DBHelper+FollowReading.h"
#import "ConstDefine.h"

@implementation OneWordASRResult
+ (NSString*)tableName{return @"wordASRResults";}
+ (NSString *)primaryKey{return @"primary key(userId,bookId,pageIndex,senIndex)";}
- (NSString *)byPrimaryKey{return [NSString stringWithFormat:@" userId='%@' and bookId = '%@' and pageIndex = '%@' and senIndex = '%@'",@(_userId),@(_bookId),@(_pageIndex),@(_senIndex)];}
@end

@implementation OneWordASRResult_Failure
+ (NSString*)tableName{return @"wordASRResults_Failure";}
+ (NSString *)primaryKey{return @"primary key(userId,bookId,pageIndex,senIndex)";}
- (NSString *)byPrimaryKey{return [NSString stringWithFormat:@" userId='%@' and bookId = '%@' and pageIndex = '%@' and senIndex = '%@'",@(_userId),@(_bookId),@(_pageIndex),@(_senIndex)];}
@end


@implementation DBHelper (FollowReading)

ASSOCIATED(ASRResultdbQue, setASRResultdbQue, JRFMDatabaseQueue*, OBJC_ASSOCIATION_RETAIN_NONATOMIC)

+ (void)load{
    [self swizzleSelector:@selector(init) withSelector:@selector(init_ASRResultDB)];
}

- (void)update_VersionForSMSDB:(JRFMDatabase*)db
{
    long ver = [JRFMDatabase JRFMDB_getVersionFromDB:db];
    if (ver <= 0) {
        [JRFMDatabase JRFMDB_setDB:db version:2];//Version 3.0.0开发使用 //1->2.0.1
    }
}


- (instancetype)init_ASRResultDB
{
    self = [self init_ASRResultDB];
    if (self) {
        BOOL debugDB = YES;
#if __KeyForDB_ON_
        debugDB = NO;
#endif
        NSString* path = [self getDBPath:__DBFileName_FollowReading debugPath:__DBFileName_FollwoReading_debug dbKey:__KeyForDB_ dbebug:debugDB];
        [self dbFile_initForDBPath:path];
        self.ASRResultdbQue = [JRFMDatabaseQueue databaseQueueWithPath:path];
        [self.ASRResultdbQue inDatabase:^(JRFMDatabase *db) {
#if __KeyForDB_ON_
            if (![db setKey:__KeyForDB_]) {
                NSLog(@"setkey failed");
            };
#endif
            //
            [db executeUpdate:[OneWordASRResult createTable]];
            [db executeUpdate:[OneWordASRResult_Failure createTable]];
            //
            [self update_VersionForSMSDB:db];
        }];
        //
    }
    return self;
}

//添加跟读数据
- (void)followReading_updateListToTableClass:(NSString *)tableClassName dictArr:(NSArray*)dictList complete:(void (^)(void))comp{
    [DataProcess do_Async_quick:^{
        [self.ASRResultdbQue inTransaction:^(JRFMDatabase *db, BOOL *rollback) {
            [self updateListToTableClass:tableClassName dictArr:dictList db:db];
            if (comp) {
                [DataProcess do_Main_quick:^{
                    comp();
                }];
            }
        }];
    }];
}
//删除跟读数据
- (void)followReading_deletListToTableClass:(NSString *)tableClassName dictArr:(NSArray*)dictList complete:(void (^)(void))comp{
    [DataProcess do_Async_quick:^{
        [self.ASRResultdbQue inTransaction:^(JRFMDatabase *db, BOOL *rollback) {
            [self deletListToTableClass:tableClassName dictArr:dictList db:db];
            if (comp) {
                [DataProcess do_Main_quick:^{
                    comp();
                }];
            }
        }];
    }];
}
- (void)followReading_deletListToTableClass:(NSString *)tableClassName tbArr:(NSArray*)dictList complete:(void (^)(void))comp{
    [DataProcess do_Async_quick:^{
        [self.ASRResultdbQue inTransaction:^(JRFMDatabase *db, BOOL *rollback) {
            [self deletListToTableClass:tableClassName tbArr:dictList db:db];
            if (comp) {
                [DataProcess do_Main_quick:^{
                    comp();
                }];
            }
        }];
    }];
}

//查询跟读数据
- (void)followReading_getDictList:(void(^)(NSArray* list))retlist fromTableClass:(NSString*)tableClassName where:(NSString*)wheresql{
    [DataProcess do_Async_quick:^{
        [self.ASRResultdbQue inTransaction:^(JRFMDatabase *db, BOOL *rollback) {
            NSArray * arr = [self getDictList_fromTableClass:tableClassName where:wheresql db:db];
            [DataProcess do_Main_quick:^{
                retlist(arr);
            }];
        }];
    }];
}

- (void)followReading_getList:(void(^)(NSArray* list))retlist fromTableClass:(NSString*)tableClassName where:(NSString*)wheresql{
    [self followReading_getDictList:^(NSArray *list) {
        [DataProcess do_Async_quick:^{
            NSMutableArray* marr = [NSMutableArray new];
            [list enumerateObjectsUsingBlock:^(NSDictionary*  _Nonnull dict, NSUInteger idx, BOOL * _Nonnull stop) {
                id obj = [NSClassFromString(tableClassName) JRMJ_objectWithKeyValues:dict];
                if ([obj isKindOfClass:[TABLE_base class]]) {
                    NSDictionary* replaceKeys = [[obj class] replacedKeyFromPropertyName];
                    for (NSString* key in replaceKeys) {
                        id value = dict[key];
                        if (value && [obj hasProperty:key]) {
                            [obj setValue:value forKey:key];
                        }
                    }
                }
                if (obj) {
                    [marr addObject:obj];
                }
            }];
            [DataProcess do_Main_quick:^{
                retlist(marr);
            }];
        }];
    } fromTableClass:tableClassName where:wheresql];
}



@end
